﻿using Digitalmes.Services.Tenant;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc.Filters;

namespace Digitalmes.ActionFilters
{

    // // 注册读写过滤器
    //services.AddScoped<ReadOperationFilter>();
    
    //// MVC配置
    //services.AddMvc(options =>
    //{
    //    options.Filters.Add<ReadOperationFilter>();
    //});

public class ReadOperationFilter : IActionFilter
    {
        private readonly ITenantProvider _tenantProvider;

        public ReadOperationFilter(ITenantProvider tenantProvider)
        {
            _tenantProvider = tenantProvider;
        }

        public void OnActionExecuting(ActionExecutingContext context)
        {
            // 标记读操作 (GET请求自动识别为读)
            var isRead = context.HttpContext.Request.Method == HttpMethods.Get;
            _tenantProvider.SetReadOperation(isRead);
        }

        public void OnActionExecuted(ActionExecutedContext context)
        {
            // 重置状态
            _tenantProvider.SetReadOperation(false);
        }
    }
}
